package cn.zust.ly.controller;

import cn.zust.ly.controller.base.CommonUpload;
import cn.zust.ly.dto.SessionInfo;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Title: 蓝拓易<br>
 * Description: 文件上传<br>
 * Copyright: Copyright (c) 2009<br>
 * Company: DCS<br>
 * 
 * @author alsa
 * @version 1.0
 */
@Controller
@RequestMapping("/up")
public class UploadController {

	private Logger logger = LoggerFactory.getLogger(UploadController.class);

	/** 限制文件大小 */
	private static int LIMITSIZE = 10 * 1024 * 1000;

	/** 临时路径 */
	public static final String TEMPDIR = "D:\\tmp";

	/**
	 * 演示页面图片上传
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/onePic")
	public @ResponseBody
	Map<String, Object> test(HttpSession session,HttpServletRequest request) {
//		SessionInfo sessionInfo = (SessionInfo) request.getSession()
//				.getAttribute("sessionInfo");
		int userId = ((SessionInfo)session.getAttribute("sessionInfo")).getUserId();
		return upload(session, request, "userPic/" + userId, LIMITSIZE);
	}







	public static Map<String, Object> upload(
			HttpSession session, HttpServletRequest request, String module, int size) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String files = CommonUpload.upload(session, request, module, size);
			map.put("success", true);

			map.put("files", files);
			return map;
		} catch (Exception e) {
			//logger.error("文件保存失败", e);
			map.put("errorInfo", "文件保存失败");
			map.put("success", false);
			return map;
		}
	}


}
